import allure
from appium.webdriver.common.appiumby import AppiumBy

from ..base.base import Base


class AddMemberManualPage(Base):
    __INPUT_NAME = (AppiumBy.XPATH, '//*[contains(@text,"姓名")]/../*[@text="必填"]')
    __INPUT_PHONE = (AppiumBy.XPATH, '//*[contains(@text,"手机")]/..//*[@text="必填"]')
    __AUTO_INVITE = (AppiumBy.ID, 'com.tencent.wework:id/hvj')
    __SAVE_BUTTON = (AppiumBy.XPATH, "//*[@text='保存']")
    __TOAST = (AppiumBy.XPATH, "//*[@class='android.widget.Toast']")
    __FAIL_DIALOG = (AppiumBy.ID, "com.tencent.wework:id/cn1")
    __SAVE_CONTINUE_ADD_BUTTON = (AppiumBy.ID, "com.tencent.wework:id/b0c")

    @allure.step("输入姓名、手机")
    def add_member_info(self, name, phone):
        self.find_ele_wait_until_located(self.__INPUT_NAME).send_keys(name)
        self.find_ele_wait_until_located(self.__INPUT_PHONE).send_keys(phone)

        ele_invite = self.find_ele(self.__AUTO_INVITE)
        if ele_invite.is_selected():
            ele_invite.click()

        image_path = self.screenshot()
        allure.attach.file(image_path, name="输入姓名、手机", attachment_type=allure.attachment_type.PNG)

    @allure.step("点击保存按钮")
    def save_button(self):
        self.find_ele(self.__SAVE_BUTTON).click()

    @allure.step("点击保存并继续添加")
    def continue_save_button(self):
        self.find_ele(self.__SAVE_CONTINUE_ADD_BUTTON).click()

    @allure.step("成功添加成员")
    def save_member(self, name, phone):
        self.add_member_info(name, phone)
        self.save_button()
        from .contact_page import ContactPage
        return ContactPage(self.driver)

    @allure.step("继续添加成员")
    def save_member_continue(self, name, phone):
        self.add_member_info(name, phone)
        self.continue_save_button()
        name_tmp = name + '1'
        self.add_member_info(name_tmp, phone)
        self.save_button()
        return self

    @allure.step("获取toast提示")
    def get_toast(self):
        toast_text = self.find_ele(self.__TOAST).text
        image_path = self.screenshot()
        allure.attach.file(image_path, name="获取toast提示", attachment_type=allure.attachment_type.PNG)
        return toast_text

    @allure.step("获取dialog提示")
    def get_dialog(self):
        self.find_ele_wait_until_located(self.__FAIL_DIALOG)
        dialog_text = self.find_ele(self.__FAIL_DIALOG).text
        image_path = self.screenshot()
        allure.attach.file(image_path, name="获取toast提示", attachment_type=allure.attachment_type.PNG)
        return dialog_text
